﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace SqlMapper.SqlElements
{
    /// <summary>
	/// SqlTag is a children element of dynamic Sql element.
	/// SqlTag represent any binary unary/conditional element (like isEmpty, isNull, iEquall...) 
	/// or other element as isParameterPresent, isNotParameterPresent, iterate.
	/// </summary>
	[Serializable]
    public abstract class BaseTag : ISqlChild, IDynamicParent
    {
        public BaseTag()
        {
            Prepend = string.Empty;
        }
        #region Fields

        [NonSerialized]
        private IList _children = new ArrayList();
        [NonSerialized]
        private string _property = string.Empty;

        #endregion

        /// <summary>
        /// Parent tag element
        /// </summary>
        [XmlIgnoreAttribute]
        public BaseTag Parent { get; set; }

        /// <summary>
		/// Property attribute
		/// </summary>
		[XmlAttribute("property")]
        public string Property
        {
            get
            {
                return _property;
            }
            set
            {
                _property = value;
            }
        }

        /// <summary>
        /// Prepend attribute
        /// </summary>
        [XmlAttribute("prepend")]
        public string Prepend { get; set; }
        
        public string Sql { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public bool IsPrependAvailable
        {
            get
            {
                return (Prepend != null && Prepend.Length > 0);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator GetChildrenEnumerator()
        {
            return _children.GetEnumerator();
        }

        #region IDynamicParent Members

        public void AddChild(ISqlChild child)
        {
            if (child is BaseTag)
            {
                ((BaseTag)child).Parent = this;
            }
            _children.Add(child);
        }

        public virtual bool IsCondition(string value)
        {
            return false;
        }

        #endregion
    }
}
